package com.dao.impl;

import com.dao.IStudentsDAO;
import com.vo.Students;
import com.db.ConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.*;

@Repository
public class StudentsDAOImpl implements IStudentsDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Students validateStudent(String studentId, String studentPwd) {
        String sql = "SELECT studentId, studentName FROM students WHERE studentId = ? AND studentPwd = ?";
        try {
            return jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
                Students student = new Students();
                student.setStudentId(rs.getString("studentId"));
                student.setStudentName(rs.getString("studentName")); // 使用正确字段名
                return student;
            }, studentId, studentPwd);
        } catch (EmptyResultDataAccessException e) {
            return null;  // 找不到用户返回null
        }
    }
}